import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ConfigService } from 'app/config/config.service';

// 设备类型分类枚举
export enum EQUIPMENT_CODE {
    CCTV = 3,
    VMS = 0,
    VD = 1,
    TSC = 4
}
// VMS:0，AID:1，LPR:2，CCTV:3，TSC:4

// Incident分类枚举
export enum INCIDENT_CODE {
    ACCIDENT = 1,
    BAD_WEATHER = 2,
    CONGESTION = 3,
    FIRE = 4,
    OBSTACLE = 5,
    REVERSE_FLOW = 6,
    ROADWORKS = 7,
    ROADMAINTENANCE = 8
}

// incident verify status
export enum IR_Verify_Status {
    Anticipated = 0,
    Notified = 1,
    Confirmed = 2,
    InProgress = 3,
    Over = 4
}

// 基础图层分类枚举
export const enum BASIC_LAYER_KEY {
    BASIC_LAYER = 'basic_layer',
    MARKER_LAYER = 'marker_layer',
    ROAD_LAYER = 'road_layer'
}

// 功能图层分类枚举
export const enum ACTION_LAYER_KEY {
    INCIDENT_RECORD = 'Incident_Record',
    /* EQUIPMENT_CCTV = 'Equipment_CCTV',
	EQUIPMENT_VMS = 'Equipment_VMS',
	EQUIPMENT_VD = 'Equipment_VD',
	EQUIPMENT_TSC = 'Equipment_TSC', */
    EQUIPMENT = 'Equipment',
    TRAFFIC_DATA = 'Traffic_Data',
    AVL = 'AVL',
    ZONE = 'ZONE',
    USER_DEFINE_AREA = 'User_Define_Area'
}

// 特征分类 4 种
export enum MARKER_TYPE {
    // marker layer's marker
    LOCATE_MARKER = 'locate-marker',
    EQUIP_POS_MARKER = 'equip-pos-marker',
    EQUIP_ALARM_MARKER = 'equip-alarm-marker',
    ALERT_MARKER = 'alert-marker',
    INCIDENT_MARKER = 'incident-marker',
    RESPONSE_MARKER = 'response-marker',

    // equip legend marker
    /* CCTV_LEGEND_MARKER = 'CCTV-legend-marker',
	VMS_LEGEND_MARKER = 'VMS-legend-marker',
	VD_LEGEND_MARKER = 'VD-legend-marker',
	TSC_LEGEND_MARKER = 'TSC-legend-marker', */
    EQ_LEGEND_MARKER = 'EQ-legend-marker',

    // Incident legend marker
    IR_LEGEND_MARKER = 'IR-legend-marker'
}

// 弹窗分类
export enum OVERLAY_KEY {
    LOCATE_MARKER_OVERLAY = 'locate-marker-overlay',
    ALERT_OVERLAY = 'alert-overlay',
    CLUSTER_FEATURES_OVERLAY = 'cluster-features-overlay',
    INCIDENT_OVERLAY = 'incident-overlay',
    EQUIPMENT_OVERLAY = 'equipment-overlay',
    RESPONSE_OVERLAY = 'response-overlay'
}

// incident marker to png { }
export const MarkerToPng_Event = {
    '0': 'alert',
    '1': 'accident',
    '2': 'bad_weather',
    '3': 'congestion',
    '4': 'fire',
    '5': 'obstacle',
    '6': 'reverse_flow',
    '7': 'roadworks',
    '8': 'road_maintenance'
};

export interface FeatureType {
    longitude?: number | string;
    latitude?: number | string;
    status?: string; // 设备状态
    deviceType?: number;
    eqtId?: string;
    type?: string | number; // 事件类型
    available?: boolean; // feature是否可用
    linkID?: string;
    roadName?: string;
    currentDisplayUrl?: string;
}

export enum CreateEvent {
    Equipment = 'Equipment',
    Incident = 'Incident',
    ScheduleEvent = 'ScheduleEvent'
}

export interface CreateEventOptData {
    module: string;
    pageKey: string;
    path: string;
}

export const CreateEventMap: Map<CreateEvent, CreateEventOptData> = new Map([
    [
        CreateEvent.Equipment,
        {
            module: 'equipment-management',
            pageKey: '4-1',
            path: '/ccm/equipment-management/equipment-list'
        }
    ],
    [
        CreateEvent.Incident,
        {
            module: 'traffic-management',
            pageKey: '3-2',
            path: '/ccm/traffic-management/incident-record'
        }
    ],
    [
        CreateEvent.ScheduleEvent,
        {
            module: 'traffic-management',
            pageKey: '3-4',
            path: '/ccm/traffic-management/scheduled-event'
        }
    ]
]);

// const GEOSERVICE = 'http://192.168.2.26:8080';
// const GEOSERVICE = 'http://192.168.0.57:8080';
const GEOSERVICE = 'http://192.168.0.59:8080';

@Injectable()
export class MapDataService {
    private _alert = this.config.apiUrl.alert;
    private _equipment = this.config.apiUrl.equipment;
    private vnqntscmgt = this.config.apiUrl.vnqntscmgt;
    private _incident = this.config.apiUrl.incident;
    private _geoServiceUrl = this.config.apiUrl.gis;
    private zone = this.config.apiUrl.zone;
    constructor(private http: HttpClient, private config: ConfigService) {}

    /**
     * get equipment info
     * @param param EQUIPMENT_CODE
     */
    getEquipmentFeatureData(param: EQUIPMENT_CODE[]): Observable<any> {
        /* const url = '/content/data/CCTV.json';
		return this.http.get(url); */
        let types = '0,1,3,4';
        if (param.length > 0) {
            types = param.toString();
        }
        const url = `${this._equipment}/equipMgt/equip/getDeviceInfoByType?eqtTypes=${types}`;
        return this.http.get<any>(url);
    }

    /**
     * get incident info
     * @param params INCIDENT_CODE[]
     */
    getIRFeatureData(param: INCIDENT_CODE[]): Observable<any> {
        const url = `${this._incident}/vnqnincidentmgt/getIRByType?type=${param.toString()}`;
        return this.http.get<any>(url);
    }

    /**
     * alert ack
     * @param param{ alertId, ackValue, userName }
     */
    alertAck(params: any): Observable<any> {
        const { alertId, ackValue, userName } = params;
        const url = `${this._alert}/vnqnalertmgt/trafficAlert/ack?alertId=${alertId}&ackValue=${ackValue}&userName=${userName}`;
        return this.http.get<any>(url);
    }

    /**
     * get local info
     */
    getLocalInfo(coordinates: number[]) {
        const point = coordinates[0].toPrecision(9) + ',' + coordinates[1].toPrecision(9);
        const filters =
            '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><BBOX><PropertyName>geom</PropertyName><gml:Box><gml:coordinates>' +
            point +
            '</gml:coordinates></gml:Box></BBOX></Filter>';
        const url =
            this._geoServiceUrl +
            // GEOSERVICE +
            '/geoserver/HaLong/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=HaLong:VMS_export_view&maxFeatures=50&outputFormat=application%2Fjson&FILTER=' +
            filters;
        return this.http.get<any>(url);
    }
    /**
     * get Device info
     */
    getDeviceInfoByIdAndType({ fieldId = null, deviceType = null } = {}): Observable<any> {
        const url = `${this._equipment}/equipMgt/equip/getDeviceInfoByIdAndType?fieldId=${fieldId}&deviceType=${deviceType}`;
        return this.http.get<any>(url);
    }

    /**
     * get Device info
     */
    getScatsData(siteId): Observable<any> {
        // const url = `http://192.168.0.56:9574/vnqntscmgt/scats/getScatsData?siteId=${siteId}`;
        const url = `${this.vnqntscmgt}/vnqntscmgt/scats/getScatsData?siteId=${siteId}`;
        return this.http.get<any>(url);
    }
    getRoad(params): Observable<any> {
        // http://192.168.0.56:9575/equipMgt/swagger-ui.html#!/357742279131649297023583127714Controller/getPointByRoadNameUsingGET
        const { roadName } = params;
        // tslint:disable-next-line:max-line-length
        const url = `${
            this.config.apiUrl.gis
            // tslint:disable-next-line:max-line-length
        }/geoserver/HaLong/ows?service=WFS&version=2.0.0&request=GetFeature&typeName=HaLong%3AVMS_export_view&outputFormat=application%2Fjson&cql_filter=name='${encodeURIComponent(
            roadName
        )}'`;
        return this.http.get<any>(url);
    }

    // params = { posX: longitude, posY: latitude }
    // http://192.168.0.84:9586/vnqnzonemgt/vnqnzonemgt/zonemgt/getZoneIdByPoint
    getZoneIdFromGis(params): Observable<any> {
        const url = `${this.zone}/vnqnzonemgt/zonemgt/getZoneIdByPoint`;
        return this.http.post<any>(url, params);
    }
}
